package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.SingleInputDialog;
import com.dlc.felear.xiaoensale.dao.FlavourSpeedDao;
import com.dlc.felear.xiaoensale.entity.FlavourSpeed;
import com.dlc.felear.xiaoensale.entity.Operator;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.utils.NumToStringUtil;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


public class TiaoLiaoSonserSpeedAdapter extends BaseAdapter implements View.OnClickListener,SingleInputDialog.SingleDialogCallback{

    private static final String TAG = "TiaoLiaoSpeedAdapter";
    // 上下文
    private Context context;
    // 数据
    private List<FlavourSpeed> data;
    private SingleInputDialog singleInputDialog;
    private FlavourSpeedDao flavourSpeedDao;
    private int currentIndex=0;


    public TiaoLiaoSonserSpeedAdapter(Context context, List<FlavourSpeed> data) {
        if(data==null){
            data = new ArrayList<>();
        }
        this.data = data;
//        Log.e(TAG,"data="+ JsonHelper.toJson(data));
        this.context = context;
        singleInputDialog=new SingleInputDialog(context,this);
        singleInputDialog.setEtInputType((InputType.TYPE_NUMBER_FLAG_DECIMAL));
        singleInputDialog.setTips("数值");
        flavourSpeedDao=new FlavourSpeedDao(context);

    }


    public List<FlavourSpeed> getData() {
        return data;
    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Integer getItem(int position) {
        return 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.adapter_flavour_item, null);
            holder.tv_test = convertView.findViewById(R.id.tv_test);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_value = convertView.findViewById(R.id.tv_value);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        FlavourSpeed f=data.get(position);
        Log.e(TAG,"FlavourSpeed="+ JsonHelper.toJson(f));
        holder.tv_name.setText("流量计("+f.getName()+")毫升/秒");
        holder.tv_value.setText(NumToStringUtil.spitDouble(f.getStdData(),3));
        holder.tv_value.setTag(R.id.tv_value,position);
        holder.tv_value.setOnClickListener(this);
        holder.tv_test.setTag(R.id.tv_test,position);
        holder.tv_test.setOnClickListener(this);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.tv_value){
            int p=(int)v.getTag(R.id.tv_value);
            currentIndex=p;
            Log.e(TAG,"p="+p);
            FlavourSpeed f=data.get(p);
            if(f.getType()==1){
                singleInputDialog.setHintMsg("请输入重量(克)");
            }else {
                singleInputDialog.setHintMsg("请输入毫升数");
            }
            singleInputDialog.setCarNo(NumToStringUtil.spitDouble(f.getSpeed(),3));
            singleInputDialog.showDialog(R.id.tv_value);
        }else if(v.getId()==R.id.tv_test){
            int p=(int)v.getTag(R.id.tv_test);
            Log.e(TAG,"p="+p);
            currentIndex=p;
            FlavourSpeed f=data.get(p);
            if(f.getType()==1){
                singleInputDialog.setHintMsg("请输入重量(克)");
            }else {
                singleInputDialog.setHintMsg("请输入毫升数");
            }
            singleInputDialog.showDialog(R.id.tv_test);
        }
    }

    @Override
    public void SingleDialogInputResult(String value, int id) {
        if(id==R.id.tv_value){
            saveValue(value);
        }
        if(id==R.id.tv_test){
            sendValue(value);
        }
    }

    private void sendValue(String value){
        try {
            Log.e(TAG," sendValue="+value);
            if(TextUtils.isEmpty(value)){
                value="0";
            }
            FlavourSpeed f=data.get(currentIndex);
            int plus=Integer.parseInt(value);
            if(f.getType()==1){
                singleInputDialog.setHintMsg("请输入重量(克)");
                sendCmd(getOperator(f.getName(),plus,0));
            }else {
                singleInputDialog.setHintMsg("请输入毫升数");
                sendCmd(getOperator(f.getName(),0,plus));
            }
        }catch (Exception e){
            showToast("请输入有效数字");
        }
    }

    private void saveValue(String value){
        try {
            Log.e(TAG," saveValue="+value);
            if(TextUtils.isEmpty(value)){
                value="0";
            }
            FlavourSpeed f=data.get(currentIndex);
            f.setSpeed(Float.parseFloat(value));
            flavourSpeedDao.update(f);
            f=flavourSpeedDao.queryForId(f.getId());
            this.data.remove(currentIndex);
            this.data.add(currentIndex,f);
            notifyDataSetChanged();
        }catch (Exception e){
            showToast("请输入有效数字");
        }
    }

    static class ViewHolder {
        TextView tv_name, tv_value,tv_test;
    }

    private Operator getOperator(String name, int value1, int value2) {
        Operator operator = new Operator();
        operator.setName(name);
        operator.setNum(currentIndex+1);
        operator.setValue(value1);
        operator.setValue1(value2);
        return operator;
    }
    private void sendCmd(Operator operator) {
        EventBus.getDefault().post(operator, Constant.ACTION_CMD_INFO_DEBUG);
    }

    private void showToast(String msg){
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
    }

}